
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

//Speichert die Auktionen
public class AuctionManager {
	//AuktionDB
	private ArrayList<Auction> adb = new ArrayList<Auction>();
	private int acID=0;
	public AuctionManager(){
		
	}
	public synchronized void deleteAuct(int id){
		//Auktion wird aus der Auktionsdatenbank (adb) geloescht
		Auction auct = getAuct(id);
		int i=0;
		if (!(auct==null)){
			i=adb.indexOf(auct);
			adb.remove(i);
		}	
	}
	//Neue Auktion
	public synchronized String createAuction(int duration, String description, User creator){
		acID++;
		String end;
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm z");
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, duration);
		end = dateFormat.format(calendar.getTime());
		
		Auction newauct = new Auction(acID,duration,description,creator,end,this);
		adb.add(newauct);	
	
		return "An auction '"+description+"' with id "+newauct.getId()+" has been created and will end on "+end+".";
	}
	public ArrayList<Auction> getAuctlist(){
		return adb;
	}
	//Gibt Auktion mit ID X zurueck
	public Auction getAuct(int id){
		for (int i=0; i<adb.size();i++){
			if(adb.get(i).getId()==id){
				return adb.get(i);
			}
		}
		return null;
	}
	//Fuer Server shutdown
	public void StopAllforShutdown(){
		for (int i=0;i<adb.size();i++){
			adb.get(i).stopAuct();
		}
	}

}
